#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
#include <ctype.h>

/**
  * @brief   str 2 hex function
  * @example  1a -> 0x1a
  *           0X9B, -> 0x9b
  *           0xab 0xcd   --> 0xcdab
  */
static int str2hex(char *str, int *hex, int *is_valid)
{
	int val = 0;
	int valid = 0;
	char *p = str;

FILTER_STR:
    /* check if is one of 0 1 2 3 4 5 6 7 8 9 a b c d e f A B C D E F */
	while((isxdigit(*p) == 0) && (*p != 0))
	{
		p++;
	}

	valid = 0;

	while(*p != 0)
	{	
		if(isdigit(*p))	                /* 0 1 2 3 4 5 6 7 8 9 */
		{
			if(((*p == '0') && (*(p+1) == 'x')) || ((*p == '0') && (*(p+1) == 'X'))) /* '0x' '0X' */
			{
				p += 2;
				goto FILTER_STR;	
			}	
			val <<= 4;
			val |=  (*p - '0');
			valid = 1;
		}
		else if(isupper(*p))            /* A B C D E F */
		{
			val <<= 4;
			val |=  (*p - 'A') + 10;
			valid = 1;
		}
		else if(islower(*p))           /* a b c d e f */
		{
			val <<= 4;
			val |=  (*p - 'a') + 10;
			valid = 1;
		}
		else
		{
			break;	
		}
		p++;
	}

	*hex = val;
    *is_valid = valid;

	return (p-str); 
}


/*  ----------------------------------------example-------------------------------------------------
	offset =    4 "0x1b                                                                  " hex = 1b
	offset =   13 "     0x0f                                                             " hex = f
	offset =   18 " 0xdd                                                                 " hex = dd
	offset =   32 "          0x3a                                                        " hex = 3a
	offset =   35 " 51                                                                   " hex = 51
	offset =   37 " 5                                                                    " hex = 5
	offset =   40 " 55                                                                   " hex = 55
	offset =   42 " 5                                                                    " hex = 5
	offset =   47 " 0XA0                                                                 " hex = a0
	offset =   53 "  0xB1                                                                " hex = b1
	offset =   68 "           0Xcc                                                       " hex = cc
	offset =   73 " 0xDD                                                                 " hex = dd
	offset =   87 " 0X123456bCDeF                                                        " hex = 456bcdef
	offset =   90 " 11                                                                   " hex = 11
	offset =   93 " 22                                                                   " hex = 22
	offset =   96 " 33                                                                   " hex = 33
	offset =  102 "    44                                                                " hex = 44
	offset =  105 " aA                                                                   " hex = aa
	offset =  116 " 0x0x0x1b2F                                                           " hex = 1b2f
	offset =  132 "         0x0x0x 0x0Xffd                                                      " hex = ffd
	offset =  162 "   issssssssssssss ddddddddddd                                        " hex = dddddddd
	offset =  188 " eeeeeee555555555555555555                                            " hex = 55555555
*/
int main(int argc, char* argv[])
{
	char* hex_str = "0x1b     0x0f 0xdd          0x3a 51 5 55 5 0XA0  0xB1           0Xcc 0xDD 0X123456bCDeF 11 22 33    44 aA 0x0x0x1b2F \
	0x0x0x 0x0Xffd   issssssssssssss ddddddddddd eeeeeee555555555555555555 \
	";

	int offset = 0;
	int hex = 0;
	char str[100];
	int len = 0;
	int valid = 0;

	while(offset < strlen(hex_str))
	{
		len = str2hex(hex_str+offset, &hex, &valid);
		strncpy(str, hex_str+offset, len);
		str[len] = 0;
		offset += len;
		if (valid)
		{
			printf("offset = %4d \"%-70s\" hex = %x \r\n", offset, str, hex);
		}
	}

	return 0;
}